package org.example.myleet.p883;

public class Solution {
    public int projectionArea(int[][] grid) {
        int n = grid.length;
        int[] xzProject = new int[n];
        int[] yzProject = new int[n];
        int sum = 0;
        for (int x = 0; x < n; ++x) {
            for (int y = 0; y < n; ++y) {
                //分别向y轴和x轴投影
                xzProject[x] = Math.max(xzProject[x], grid[x][y]);
                yzProject[y] = Math.max(yzProject[y], grid[x][y]);
                if (grid[x][y] > 0) {
                    //统计xy平面投影的面积
                    ++sum;
                }
            }
        }
        for (int z : xzProject) {
            sum += z;
        }
        for (int z : yzProject) {
            sum += z;
        }
        return sum;
    }
}
